package mn.slide.laborberlin.main;

import mn.slide.laborberlin.model.PlistModel;
import mn.slide.laborberlin.tabgroup.GroupStarter;
import mn.slide.laborberlin.tabgroup.HomeGroupActivity;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class AnsprechparterDetialActivity extends Activity
{
	private PlistModel	model;
	private TextView	title;
	private TextView	name1;
	private TextView	name2;
	private TextView	address1;
	private TextView	address2;
	private TextView	tel1;
	private TextView	tel2;
	private TextView	tel3;
	private Button		zum_konkakt;
	private ListView	listV;
	private String[]	mStrings	= {};
	// private Button back_btn;

	EditText			search_edt;

	@Override
	protected void onCreate( Bundle savedInstanceState )
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.home);
		Bundle extra = getIntent().getExtras();
		if (extra != null)
			model = extra.getParcelable("model");
		setContentView(R.layout.home);
		listV = (ListView) findViewById(R.id.main_list);
		View footerView = ((LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.ansprechparter_detial, null, false);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mStrings);
		listV.addFooterView(footerView);
		listV.setAdapter(adapter);
		title = (TextView) findViewById(R.id.header_title);
		name1 = (TextView) findViewById(R.id.name1);
		name2 = (TextView) findViewById(R.id.name2);
		address1 = (TextView) findViewById(R.id.address1);
		address2 = (TextView) findViewById(R.id.address2);
		tel1 = (TextView) findViewById(R.id.telefon1);
		tel2 = (TextView) findViewById(R.id.telefon2);
		tel3 = (TextView) findViewById(R.id.telefon3);
		zum_konkakt = (Button) findViewById(R.id.zum_konkakt);
		zum_konkakt.setOnClickListener(new View.OnClickListener()
		{

			@Override
			public void onClick( View v )
			{

				Intent intent = new Intent(Intent.ACTION_INSERT);
				intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
				intent.putExtra(ContactsContract.Intents.Insert.NAME, model.getName1());
				intent.putExtra(ContactsContract.Intents.Insert.PHONE, model.getTel1());
				int PICK_CONTACT = 100;
				startActivityForResult(intent, PICK_CONTACT);

			}
		});
		search_edt = (EditText) findViewById(R.id.search_edt);
		search_edt.setOnKeyListener(new OnKeyListener()
		{
			public boolean onKey( View v , int keyCode , KeyEvent event )
			{
				// If the event is a key-down event on the "enter" button
				if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER))
				{
					String k = search_edt.getText().toString();
					if (k.length() > 0)
					{
						EditText searchbox = (EditText) v;
						InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
						imm.hideSoftInputFromWindow(searchbox.getWindowToken(), 0);
						Intent intent = new Intent(AnsprechparterDetialActivity.this, SearchActivity.class);
						intent.putExtra("searchTxt", k);
						HomeGroupActivity.group.goTo(intent);

					}
					return true;
				}
				return false;
			}
		});
		viewItem();
		// back_btn = (Button) findViewById(R.id.back_btn);
		// back_btn.setOnClickListener(new View.OnClickListener() {
		//
		// @Override
		// public void onClick(View v) {
		// onBackPressed();
		//
		// }
		// });

	}

	public void viewItem()
	{
		String title , name1 , name2 , address1 , address2 , tel1 , tel2 , tel3;
		title = model.getTitle();
		name1 = model.getName1();
		name2 = model.getName2();
		address1 = model.getAddress1();
		address2 = model.getAddress2();
		tel1 = model.getTel1();
		tel2 = model.getTel2();
		tel3 = model.getTel3();
		if (title != null)
		{
			this.title.setText(title);
		}
		else
		{
			this.title.setVisibility(View.GONE);
		}
		if (name1 != null)
		{
			this.name1.setText(name1);
		}
		else
		{
			this.name1.setVisibility(View.GONE);
		}
		if (name2 != null)
		{
			this.name2.setText(name2);
		}
		else
		{
			this.name2.setVisibility(View.GONE);
		}
		if (address1 != null)
		{
			this.address1.setText(address1);
		}
		else
		{
			this.address1.setVisibility(View.GONE);
		}
		if (address2 != null)
		{
			this.address2.setText(address2);
		}
		else
		{
			this.address2.setVisibility(View.GONE);
		}
		if (tel1 != null)
		{
			this.tel1.setText(tel1);
		}
		else
		{
			this.tel1.setVisibility(View.GONE);
		}
		if (tel2 != null)
		{
			this.tel2.setText(tel2);
		}
		else
		{
			this.tel2.setVisibility(View.GONE);
		}
		if (tel3 != null)
		{
			this.tel3.setText(tel3);
		}
		else
		{
			this.tel3.setVisibility(View.GONE);
		}

	}

	@Override
	public void onBackPressed()
	{

		GroupStarter.getInstance().onBackpressed(this);
	}
}
